package com.example.camerapush;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.camerapush.ptp.Camera;
import com.example.camerapush.ptp.model.ObjectInfo;
import com.luoshihai.xxphotoview.PhotoView;
import com.yanzhenjie.fragment.NoFragment;

import java.util.ArrayList;

/**
 * Author:lsh
 * Version: 1.0
 * Description:
 * Date: 2018/2/21
 */

public class BigPicFragment extends NoFragment implements View.OnClickListener {
    MainActivity mainActivity;
    private ViewPager mPager;
    private ArrayList<Integer> handles;
    private int position;
    private TextView tv_pageIndex;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.frag_bigpic, container, false);
        tv_pageIndex = ((TextView) view.findViewById(R.id.tv_pageIndex));
        mPager = (ViewPager) view.findViewById(R.id.pager);
        view.findViewById(R.id.btn_up).setOnClickListener(this);
        mainActivity = ((MainActivity) getActivity());
        initiv();
        registercheckBroadCast();
        return view;
    }

    private void registercheckBroadCast() {
        IntentFilter checkFilter = new IntentFilter();
        checkFilter.addAction(mainActivity.CHECKUPFILEACTION);
        mainActivity.registerReceiver(checkUpNumberReceiver, checkFilter);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mainActivity.unregisterReceiver(checkUpNumberReceiver);
    }

    private BroadcastReceiver checkUpNumberReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            switch (action) {
                case MainActivity.CHECKUPFILEACTION:
                    if (mainActivity.handles == null || mainActivity.handles.length == 0)
                        finish();
                    break;
            }
        }
    };

    private void initiv() {
        Bundle bundle = getArguments();
        position = bundle.getInt("index");
        handles = bundle.getIntegerArrayList("handles");
        tv_pageIndex.setText((position + 1) + "/" + handles.size());

        mPager.setAdapter(pagerAdapter);
        mPager.setOnPageChangeListener(onPageChangeListener);
        mPager.setCurrentItem(position);
    }


    ViewPager.OnPageChangeListener onPageChangeListener = new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            super.onPageSelected(position);
            BigPicFragment.this.position = position;
            tv_pageIndex.setText((position + 1) + "/" + handles.size());
        }
    };


    PagerAdapter pagerAdapter = new PagerAdapter() {

        @Override
        public int getCount() {
            return handles.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View inflate = LayoutInflater.from(container.getContext()).inflate(R.layout.activity_img, null);
            final PhotoView view = (PhotoView) inflate.findViewById(R.id.img1);
            view.enable();
            Integer handle = handles.get(position);
            view.setScaleType(ImageView.ScaleType.FIT_CENTER);
//            mainActivity.camera.retrieveImage(new Camera.RetrieveImageListener() {
//                @Override
//                public void onImageRetrieved(int objectHandle, final Bitmap image) {
//                }
//            }, handle);
            mainActivity.camera.retrieveImageInfo(new Camera.RetrieveImageInfoListener() {
                @Override
                public void onImageInfoRetrieved(int objectHandle, ObjectInfo objectInfo, final Bitmap thumbnail) {
                    mainActivity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            view.setImageBitmap(thumbnail);
                        }
                    });

                }
            }, handle);
            container.addView(inflate);
            return inflate;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_up:
                if (mainActivity.AlbumId == -1) {
                    Toast.makeText(mainActivity, "请选择上传相册", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!mainActivity.hasUpPermission) {
                    Toast.makeText(mainActivity, "你没有权限上传到该相册", Toast.LENGTH_SHORT).show();
                    return;
                }
                Toast.makeText(mainActivity, "开始上传", Toast.LENGTH_SHORT).show();
                mainActivity.addFileToQueue(handles.get(position));
                break;
        }
    }
}
